/*
 * $Id: sshc.c 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "sshc.h"

int
main(int argc, char *argv[])
{
   errno = 0;
   verbose = 0;
   got_channel = 0;
   got_password = 0;

   int port = 0;
   int af = 0;
   char *username = NULL;
   char *input = NULL;

   if (argc <= 1)
      usage();
   else {
#ifdef __OpenBSD__
      const char *errstr;
      setproctitle("%d", getpid());
#endif

      signal(SIGINT, handle_signal);
      signal(SIGHUP, handle_signal);
      signal(SIGPIPE, handle_signal);
      signal(SIGUSR1, handle_signal);
      signal(SIGTERM, handle_signal);

      int ch = 0;
      while ((ch = getopt(argc, argv, "46df:l:p:v")) != EOF) {
         switch (ch) {
            case '4':
               af = PF_INET;
               break;

            case '6':
               af = PF_INET6;
               break;

            case 'd':
               debug++;
               break;

            case 'p':
               if (isdigit((int)*optarg)) {
#ifdef __OpenBSD__
                  port = (int)strtonum(optarg, MINPORT, MAXPORT, &errstr);
#else
                  port = (int)strtol(optarg, (char **) NULL, 10);
#endif
               }
               else
                  warning(0, "-p option requires numeric argument");

#ifdef __OpenBSD__
               if (errstr)
                  error(0, "invalid port number %s, error: %s", 
                        optarg, errstr);
#endif
               break;

            case 'v':
               verbose++;
               break;

            case 'l':
               username = optarg;
               break;

            case 'f':
               input = optarg;
               break;

            default:
               usage();
               break;
         }
      }

      if ((argc - 1) != optind)
         usage();

      ssh_init(argv[optind], port, af);
   }

   ssh_session();
   ssh_userauth(username, NULL);
   ssh_getty(input, 0);

   cleanup();
   return 0;
}

void
usage()
{
   printf("Usage: %s: [-46dflpv] host\n", __progname);
   printf("options: \n");
   printf("  -4 [ use IPv4 ]\n");
   printf("  -6 [ use IPv6 ]\n");
   printf("  -d [ use syslog as debug ]\n");
   printf("  -l [ use different login name ]\n");
   printf("  -f [ use a file as input, instead of stdin ]\n");
   printf("  -p [ specify the server port, default: 22 ]\n");
   printf("  -v [ increase verbosity ]\n");
   cleanup();
   exit(1);
}
